package com.exp.senddb;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint({ "NewApi", "SimpleDateFormat" })
public class MainActivity extends Activity {
	
	private Button sendBT;
	private TextView txtRP;
	private TextView txtShow;
	private TextView txtBusNo;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		sendBT   = (Button)findViewById(R.id.sendBT);
		txtRP    = (TextView)findViewById(R.id.txtbox);
		txtShow  = (TextView)findViewById(R.id.txtShow); 
		txtBusNo = (TextView)findViewById(R.id.txtBus);
		
		
		sendBT.setOnClickListener(new View.OnClickListener() {
			
			@SuppressLint("NewApi")
			@Override
			public void onClick(View v) {
				
				if(txtRP.getText().toString().equals("")||txtBusNo.getText().toString().equals("")) {
					txtShow.setText("Cannot Send !!!!");
				}
				else {
					sendToDB();
					txtShow.setText("Sussses");
				}
				
			}
		});
		
	}
	
	private void sendToDB()  {
		
		try {
			StrictMode.ThreadPolicy policy = new 
			StrictMode.ThreadPolicy.Builder().permitAll().build();
		    StrictMode.setThreadPolicy(policy);
		    
		    Calendar c = Calendar.getInstance();
		    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		    SimpleDateFormat tf = new SimpleDateFormat("HH:mm:ss");
		    String date = df.format(c.getTime());
		    String time = tf.format(c.getTime());
			
		    String busNo = txtBusNo.getText().toString();
			//String date = //"10/12/2013";
			//String time = "13.00";
			String msg  = txtRP.getText().toString(); 
			
			//Log.d("log_err", "Date " +date);
			//txtShow.setText(date+"    "+time);
		 
			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		
			nameValuePairs.add(new BasicNameValuePair("check","true"));
			
			nameValuePairs.add(new BasicNameValuePair("bus_line", busNo));
		
			nameValuePairs.add(new BasicNameValuePair("date",date));
			
			nameValuePairs.add(new BasicNameValuePair("time", time));
		
			nameValuePairs.add(new BasicNameValuePair("detail",msg));
		
			HttpClient httpclient = new DefaultHttpClient();
		
			HttpPost httppost = new HttpPost("http://161.246.60.179/itone/php_set_data.php");
		
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
		
			httpclient.execute(httppost);
			
			txtRP.setText("");
			txtBusNo.setText("");
		}
		catch(Exception e){
    		Log.d("log_err", "Error in http connection " + e.toString());
    	}
					
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
